package com.pinc.exam;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Widget_Window extends LinearLayout {
	private Context mContext;
	private LayoutInflater mInflater;
	private RelativeLayout mTop;
	private LinearLayout[] mLayout;
	private boolean mShow;
	private ImageView iv_zoom;
	private boolean mDown;

	public Widget_Window(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mShow = true;
		mDown = true;
		setBackgroundDrawable(context.getResources().getDrawable(R.drawable.widget_background));
		setOrientation(LinearLayout.VERTICAL);
		setPadding(2, 2, 2, 2);
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTop = (RelativeLayout) mInflater.inflate(R.layout.widget_header, new RelativeLayout(context));
		iv_zoom = (ImageView) mTop.findViewById(R.id.imageView_zoom);
		setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				zoom();
			}
		});
		addView(mTop);
	}

	public void init(String title, String[] keys, boolean down)
	{
		mDown = down;
		init(title, keys);
	}

	public void init(String title, String[] keys)
	{
		((TextView)mTop.findViewById(R.id.textView_title)).setText(title);

		int amount = keys.length;
		if ( amount > 0)
		{
			mLayout = new LinearLayout[amount];
			for ( int i=0; i<amount;i++)
			{
				mLayout[i] = (LinearLayout) mInflater.inflate(R.layout.widget_row, new LinearLayout(mContext));
				TextView tv_key = (TextView) mLayout[i].findViewById(R.id.textView_key);
				tv_key.setText(keys[i]);
				addView(mLayout[i]);
			}
		}
		iv_zoom.setImageDrawable((mShow^mDown)?getResources().getDrawable(R.drawable.zoom_up):getResources().getDrawable(R.drawable.zoom_down));
	}

	public TextView[] getValueTextViews()
	{
		TextView[] tv = new TextView[mLayout.length];
		for ( int i=0;i<tv.length;i++)
		{
			tv[i] = (TextView)mLayout[i].findViewById(R.id.textView_value);
		}
		return tv;
	}

	public void zoom()
	{
		for ( int i=0;i<mLayout.length;i++)
		{
			mLayout[i].setVisibility((mShow)?View.GONE:View.VISIBLE);
		}
		mShow = !mShow;
		iv_zoom.setImageDrawable((mShow^mDown)?getResources().getDrawable(R.drawable.zoom_up):getResources().getDrawable(R.drawable.zoom_down));
	}
}
